import {
    _ as _sfc_main$3,
    a as _sfc_main$5,
    b as _sfc_main$9
} from "./SimpleCollapse.vue_vue_type_script_setup_true_lang-rzMMFhj6.js";
import {_ as _sfc_main$8} from "./IconButton.vue_vue_type_script_setup_true_lang-jAwwuJ2U.js";
import {_ as _sfc_main$7} from "./SelectGroup.vue_vue_type_script_setup_true_lang-DYRZ60Lx.js";
import {
    _ as _export_sfc,
    a as createElementBlock,
    b as createBaseVNode,
    c as createBlock,
    d as defineComponent,
    e as ref,
    f as computed,
    F as Fragment,
    g as useRouter,
    G as api,
    h as useRoute,
    H as normalizeStyle,
    J as mergeModels,
    j as renderList,
    k as createCommentVNode,
    K as onMounted,
    L as withDirectives,
    m as useModel,
    M as vShow,
    n as normalizeClass,
    N as dayjs,
    o as openBlock,
    O as resolveDynamicComponent,
    p as withCtx,
    q as createVNode,
    r as resolveComponent,
    S as Suspense,
    t as toDisplayString,
    u as unref,
    v as createTextVNode,
    y as isRef
} from "./index-Dfdf_Rjk.js";
import {d as deleteJob, g as getJobList} from "./job-DoMclj1u.js";
import {f as formatToDateTime, g as getHashColorFromString} from "./dateUtils-CggKSslP.js";
import {_ as _sfc_main$6} from "./SimpleDrawer.vue_vue_type_script_setup_true_lang-e2z4Lw6B.js";
import {_ as _sfc_main$4, u as useCopy} from "./useCopy-DYzvKz8A.js";

function getStatusColor(status) {
    switch (status) {
        case "RUNNING":
            return "blue-badge";
        case "COMPLETED":
            return "green-badge";
        case "FAILED":
        case "CREATE_FAILED":
            return "red-badge";
        default:
            return "gray-badge";
    }
}

const _hoisted_1$1 = {class: "h-full w-640px flex flex-col"};
const _hoisted_2$1 = {
    flex: "~ gap2 none items-center justify-between",
    border: "b base",
    class: "relative h-8 bg-hover px-4"
};
const _hoisted_3$1 = {class: "font-600"};
const _hoisted_4$1 = {class: "flex-1 overflow-y-overlay p-3"};
const _sfc_main$2 = /* @__PURE__ */ defineComponent({
    __name: "job-detail-drawer",
    props: /* @__PURE__ */ mergeModels({
        job: {}
    }, {
        "modelValue": {type: Boolean},
        "modelModifiers": {}
    }),
    emits: ["update:modelValue"],
    setup(__props) {
        const props = __props;
        const show = useModel(__props, "modelValue");
        const formatedJob = computed(() => {
            if (!props.job)
                return null;
            const decodedJob = {
                ...props.job,
                sql: void 0,
                createTime: props.job.createTime ? formatToDateTime(props.job.createTime) : void 0
            };
            return JSON.stringify(decodedJob, null, 2);
        });
        const {copy, copied} = useCopy();
        const {copy: copySql, copied: sqlCopied} = useCopy();
        return (_ctx, _cache) => {
            const _component_ShikiCode = _sfc_main$4;
            const _component_SimpleCollapse = _sfc_main$3;
            const _component_Empty = _sfc_main$5;
            const _component_SimpleDrawer = _sfc_main$6;
            return openBlock(), createBlock(_component_SimpleDrawer, {
                modelValue: show.value,
                "onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => show.value = $event)
            }, {
                default: withCtx(() => {
                    var _a, _b;
                    return [
                        createBaseVNode("div", _hoisted_1$1, [
                            createBaseVNode("div", _hoisted_2$1, [
                                createBaseVNode("span", _hoisted_3$1, "任务【" + toDisplayString((_a = _ctx.job) == null ? void 0 : _a.id) + "】详情", 1)
                            ]),
                            createBaseVNode("div", _hoisted_4$1, [
                                unref(formatedJob) ? (openBlock(), createElementBlock(Fragment, {key: 0}, [
                                    createVNode(_component_SimpleCollapse, {
                                        "model-value": true,
                                        "header-class": "bg-selected"
                                    }, {
                                        title: withCtx(() => _cache[3] || (_cache[3] = [
                                            createTextVNode(" 基本信息 ")
                                        ])),
                                        actions: withCtx(() => [
                                            createBaseVNode("button", {
                                                class: "a-primary",
                                                onClick: _cache[0] || (_cache[0] = ($event) => unref(copy)(unref(formatedJob)))
                                            }, toDisplayString(unref(copied) ? "✓ 已复制" : "复制"), 1)
                                        ]),
                                        default: withCtx(() => [
                                            createVNode(_component_ShikiCode, {
                                                code: unref(formatedJob),
                                                lang: "json",
                                                class: "bg-blue/10 text-0.9rem"
                                            }, null, 8, ["code"])
                                        ]),
                                        _: 1
                                    }),
                                    ((_b = _ctx.job) == null ? void 0 : _b.sql) ? (openBlock(), createBlock(_component_SimpleCollapse, {
                                        key: 0,
                                        "model-value": true,
                                        class: "mt-3",
                                        "header-class": "bg-selected"
                                    }, {
                                        title: withCtx(() => _cache[4] || (_cache[4] = [
                                            createTextVNode(" SQL 信息 ")
                                        ])),
                                        actions: withCtx(() => [
                                            createBaseVNode("button", {
                                                class: "a-primary",
                                                onClick: _cache[1] || (_cache[1] = ($event) => {
                                                    var _a2;
                                                    return unref(copySql)((_a2 = _ctx.job) == null ? void 0 : _a2.sql);
                                                })
                                            }, toDisplayString(unref(sqlCopied) ? "✓ 已复制" : "复制"), 1)
                                        ]),
                                        default: withCtx(() => [
                                            createVNode(_component_ShikiCode, {
                                                code: _ctx.job.sql,
                                                lang: "sql",
                                                class: "bg-yellow/10 text-0.9rem"
                                            }, null, 8, ["code"])
                                        ]),
                                        _: 1
                                    })) : createCommentVNode("", true)
                                ], 64)) : (openBlock(), createBlock(_component_Empty, {
                                    key: 1,
                                    class: "h-full"
                                }, {
                                    default: withCtx(() => _cache[5] || (_cache[5] = [
                                        createTextVNode(" 暂无详情 ")
                                    ])),
                                    _: 1
                                }))
                            ])
                        ])
                    ];
                }),
                _: 1
            }, 8, ["modelValue"]);
        };
    }
});
const _hoisted_1 = {class: "h-full flex flex-col gap-3"};
const _hoisted_2 = {class: "flex items-center justify-between rounded-md bg-base px-4 py-3 color-#666"};
const _hoisted_3 = {class: "flex items-center"};
const _hoisted_4 = {key: 0};
const _hoisted_5 = {class: "text-purple-600"};
const _hoisted_6 = {class: "relative flex-1 overflow-y-overlay rounded-md bg-base p-2 pt-0"};
const _hoisted_7 = {class: "w-full"};
const _hoisted_8 = {class: "sticky top-0 z-10 border-b border-base bg-base color-base"};
const _hoisted_9 = {class: "h-10 color-gray-700 font-600"};
const _hoisted_10 = {class: "p1 text-center"};
const _hoisted_11 = {class: "p1 text-center"};
const _hoisted_12 = {class: "ws-nowrap p1 text-center"};
const _hoisted_13 = {class: "text-0.9rem color-gray"};
const _hoisted_14 = ["onClick"];
const _hoisted_15 = {class: "min-w-16 ws-nowrap text-center op50"};
const _hoisted_16 = {class: "min-w-30 ws-nowrap text-center"};
const _hoisted_17 = ["onClick"];
const _hoisted_18 = {class: "relative min-w-30 ws-nowrap text-center"};
const _hoisted_19 = {class: "h-10 min-w-30 flex-center gap-2 ws-nowrap"};
const _hoisted_20 = {class: "flex-center gap-2 text-purple-600"};
const _hoisted_21 = {class: "min-w-30 ws-nowrap text-center"};
const _hoisted_22 = {class: "min-w-40 ws-nowrap text-center"};
const _hoisted_23 = {class: "min-w-40 ws-nowrap text-center"};
const _hoisted_24 = {class: "min-w-30 flex-center gap-3 ws-nowrap text-0.9rem"};
const _hoisted_25 = ["onClick"];
const _hoisted_26 = {class: "flex items-center gap-3 p-4"};
const _hoisted_27 = ["onClick"];
const _hoisted_28 = {
    key: 0,
    class: "h-full w-full flex-center"
};
const _sfc_main$1 = /* @__PURE__ */ defineComponent({
    __name: "index",
    setup(__props) {
        const route = useRoute();
        const modelIdFromModelPage = computed(() => {
            var _a;
            return ((_a = route.query) == null ? void 0 : _a.modelIdFromModelPage) || "";
        });
        const modelNameFromModelPage = computed(() => {
            var _a;
            return decodeURIComponent(((_a = route.query) == null ? void 0 : _a.modelNameFromModelPage) || "");
        });
        const sortby = ref("order");
        const direction = ref("desc");
        const jobs = ref([]);
        const loading = ref(false);
        const currId = ref("");
        const filterStatus = ref("all");
        const statusList = computed(() => [
            {
                label: "全部任务",
                value: "all",
                list: jobs.value,
                class: "!text-primary-700 !bg-primary-400/15 !hover:bg-primary-400/15"
            },
            {
                label: "已完成",
                value: "completed",
                list: jobs.value.filter((u) => u.status === "COMPLETED"),
                class: "!text-green-600 !bg-green-400/15 !hover:bg-green-400/15"
            },
            {
                label: "运行中",
                value: "running",
                list: jobs.value.filter((u) => u.status === "RUNNING"),
                class: "!text-blue-500 !bg-blue-400/15 !hover:bg-blue-400/15"
            },
            {
                label: "失败",
                value: "failed",
                list: jobs.value.filter((u) => u.status === "FAILED"),
                class: "!text-red-500 !bg-red-400/15 !hover:bg-red-400/15"
            }
        ]);
        const sortFunctions = {
            order: (a, b) => dayjs(a.createTime).valueOf() - dayjs(b.createTime).valueOf(),
            modelName: (a, b) => a.modelName.localeCompare(b.modelName),
            createTime: (a, b) => dayjs(a.createTime).valueOf() - dayjs(b.createTime).valueOf()
        };
        const startTimes = computed(() => [...jobs.value].sort(sortFunctions.createTime).reverse().map((u) => u.createTime));
        const sorted = computed(() => {
            var _a;
            const sortFn = sortFunctions[sortby.value];
            const list = ((_a = statusList.value.find((u) => u.value === filterStatus.value)) == null ? void 0 : _a.list) || jobs.value;
            const sorted2 = [...list].sort(sortFn);
            if (direction.value === "desc")
                sorted2.reverse();
            return sorted2;
        });

        function toggleSortedBy(by) {
            if (sortby.value === by)
                direction.value = direction.value === "asc" ? "desc" : "asc";
            else
                sortby.value = by;
        }

        async function getData() {
            loading.value = true;
            const params = {
                page: 1,
                pageSize: 800
            };
            if (modelIdFromModelPage.value)
                params.modelId = modelIdFromModelPage.value;
            getJobList(params).then((res) => {
                jobs.value = res.data;
            }).catch(() => {
                jobs.value = [];
            }).finally(() => {
                loading.value = false;
            });
        }

        onMounted(getData);
        const showDetailDrawer = ref(false);
        const selectedJob = ref();

        function showJobDetail(job) {
            selectedJob.value = job;
            showDetailDrawer.value = true;
        }

        const router = useRouter();

        function showJobResult(info) {
            if (info.status !== "COMPLETED") {
                api.warn("任务未成功完成，无法查看结果");
                return;
            }
            currId.value = info.id;
            router.push({
                name: "JobResult",
                query: {
                    id: info.id,
                    modelId: info.modelId,
                    modelName: info.modelName
                }
            });
        }

        function confirmDelete(info) {
            deleteJob(info.id).then(() => {
                api.success("删除成功");
                getData();
            });
        }

        return (_ctx, _cache) => {
            const _component_SelectGroup = _sfc_main$7;
            const _component_IconButton = _sfc_main$8;
            const _component_Loading = _sfc_main$9;
            const _component_VDropdown = resolveComponent("VDropdown");
            const _component_Empty = _sfc_main$5;
            return openBlock(), createElementBlock("div", _hoisted_1, [
                createBaseVNode("div", _hoisted_2, [
                    createBaseVNode("div", _hoisted_3, [
                        unref(modelIdFromModelPage) && unref(modelNameFromModelPage) ? (openBlock(), createElementBlock("span", _hoisted_4, [
                            _cache[5] || (_cache[5] = createTextVNode(" 模型")),
                            createBaseVNode("span", _hoisted_5, "【" + toDisplayString(unref(modelNameFromModelPage)) + " - " + toDisplayString(unref(modelIdFromModelPage)) + "】", 1),
                            _cache[6] || (_cache[6] = createTextVNode("共关联： "))
                        ])) : createCommentVNode("", true),
                        createVNode(_component_SelectGroup, {
                            modelValue: unref(filterStatus),
                            "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => isRef(filterStatus) ? filterStatus.value = $event : null),
                            options: unref(statusList).map((i) => i.value),
                            titles: unref(statusList).map((i) => "".concat(i.label, "：").concat(i.list.length)),
                            props: unref(statusList).map((i) => ({
                                class: "".concat(i.value === unref(filterStatus) ? i.class : "")
                            }))
                        }, null, 8, ["modelValue", "options", "titles", "props"])
                    ]),
                    createVNode(_component_IconButton, {
                        class: "h-7 w-7",
                        "icon-class": "i-mdi:refresh",
                        title: "刷新",
                        onClick: getData
                    })
                ]),
                createBaseVNode("div", _hoisted_6, [
                    unref(loading) ? (openBlock(), createBlock(_component_Loading, {key: 0})) : (openBlock(), createElementBlock(Fragment, {key: 1}, [
                        withDirectives(createBaseVNode("table", _hoisted_7, [
                            createBaseVNode("thead", _hoisted_8, [
                                createBaseVNode("tr", _hoisted_9, [
                                    createBaseVNode("th", _hoisted_10, [
                                        createBaseVNode("button", {
                                            onClick: _cache[1] || (_cache[1] = ($event) => toggleSortedBy("order"))
                                        }, [
                                            _cache[7] || (_cache[7] = createTextVNode(" 序号 ")),
                                            unref(sortby) === "order" ? (openBlock(), createElementBlock("div", {
                                                key: 0,
                                                class: normalizeClass([unref(direction) === "asc" ? "i-carbon-arrow-down" : "i-carbon-arrow-up"])
                                            }, null, 2)) : createCommentVNode("", true)
                                        ])
                                    ]),
                                    _cache[10] || (_cache[10] = createBaseVNode("th", {class: "p1 text-center"}, " 任务 ID ", -1)),
                                    _cache[11] || (_cache[11] = createBaseVNode("th", {
                                        class: "p1 text-center",
                                        colspan: "1"
                                    }, " 执行程序 ", -1)),
                                    createBaseVNode("th", _hoisted_11, [
                                        createBaseVNode("button", {
                                            onClick: _cache[2] || (_cache[2] = ($event) => toggleSortedBy("modelName"))
                                        }, [
                                            _cache[8] || (_cache[8] = createTextVNode(" 所用模型 ")),
                                            unref(sortby) === "modelName" ? (openBlock(), createElementBlock("div", {
                                                key: 0,
                                                class: normalizeClass([unref(direction) === "desc" ? "i-carbon-arrow-down" : "i-carbon-arrow-up"])
                                            }, null, 2)) : createCommentVNode("", true)
                                        ])
                                    ]),
                                    _cache[12] || (_cache[12] = createBaseVNode("th", {class: "p1 text-center"}, " 任务状态 ", -1)),
                                    createBaseVNode("th", _hoisted_12, [
                                        createBaseVNode("button", {
                                            onClick: _cache[3] || (_cache[3] = ($event) => toggleSortedBy("createTime"))
                                        }, [
                                            _cache[9] || (_cache[9] = createTextVNode(" 开始时间 ")),
                                            unref(sortby) === "createTime" ? (openBlock(), createElementBlock("div", {
                                                key: 0,
                                                class: normalizeClass([unref(direction) === "desc" ? "i-carbon-arrow-down" : "i-carbon-arrow-up"])
                                            }, null, 2)) : createCommentVNode("", true)
                                        ])
                                    ]),
                                    _cache[13] || (_cache[13] = createBaseVNode("th", {class: "p1"}, " 数据源起止 ", -1)),
                                    _cache[14] || (_cache[14] = createBaseVNode("th", {class: "p1 text-center"}, " 操作 ", -1))
                                ])
                            ]),
                            createBaseVNode("tbody", _hoisted_13, [
                                (openBlock(true), createElementBlock(Fragment, null, renderList(unref(sorted), (item) => {
                                    return openBlock(), createElementBlock("tr", {
                                        key: item.id,
                                        class: normalizeClass(["h-10 hover:bg-hover", {"!bg-selected": unref(currId) === item.id}]),
                                        onClick: ($event) => currId.value = item.id
                                    }, [
                                        createBaseVNode("td", _hoisted_15, toDisplayString(unref(startTimes).indexOf(item.createTime) + 1), 1),
                                        createBaseVNode("td", _hoisted_16, [
                                            createBaseVNode("code", {
                                                class: "cursor-pointer underline a-primary",
                                                onClick: ($event) => showJobDetail(item)
                                            }, toDisplayString(item.id), 9, _hoisted_17)
                                        ]),
                                        createBaseVNode("td", _hoisted_18, [
                                            createBaseVNode("code", {
                                                style: normalizeStyle({color: unref(getHashColorFromString)(item.jobExecuter)})
                                            }, toDisplayString(item.jobExecuter), 5)
                                        ]),
                                        createBaseVNode("td", _hoisted_19, [
                                            createBaseVNode("div", _hoisted_20, [
                                                createBaseVNode("span", null, toDisplayString(item.modelName), 1),
                                                createBaseVNode("div", {
                                                    class: normalizeClass(["text-1.1rem text-purple-400", item.resultType === "graph" ? "i-carbon:network-4" : "i-carbon:table-split"])
                                                }, null, 2)
                                            ])
                                        ]),
                                        createBaseVNode("td", _hoisted_21, [
                                            createBaseVNode("span", {
                                                class: normalizeClass(["px-1.5 py-0.5", unref(getStatusColor)(item.status)])
                                            }, toDisplayString(item.status), 3)
                                        ]),
                                        createBaseVNode("td", _hoisted_22, toDisplayString(unref(formatToDateTime)(item.createTime)), 1),
                                        createBaseVNode("td", _hoisted_23, [
                                            createBaseVNode("code", null, toDisplayString(item.sourceDbId) + " → " + toDisplayString(item.destinationDbId), 1)
                                        ]),
                                        createBaseVNode("td", _hoisted_24, [
                                            item.status === "COMPLETED" ? (openBlock(), createElementBlock("button", {
                                                key: 0,
                                                class: "a-primary",
                                                onClick: ($event) => showJobResult(item)
                                            }, " 结果 ", 8, _hoisted_25)) : createCommentVNode("", true),
                                            item.status !== "RUNNING" ? (openBlock(), createBlock(_component_VDropdown, {
                                                key: 1,
                                                placement: "left",
                                                distance: -10
                                            }, {
                                                popper: withCtx(() => [
                                                    createBaseVNode("div", _hoisted_26, [
                                                        _cache[15] || (_cache[15] = createBaseVNode("span", null, "确认删除吗？", -1)),
                                                        createBaseVNode("button", {
                                                            class: "rounded bg-error px-2 py-1 text-sm text-white",
                                                            onClick: ($event) => confirmDelete(item)
                                                        }, " 是的 ", 8, _hoisted_27)
                                                    ])
                                                ]),
                                                default: withCtx(() => [
                                                    _cache[16] || (_cache[16] = createBaseVNode("button", {class: "a-error"}, " 删除 ", -1))
                                                ]),
                                                _: 2
                                            }, 1024)) : createCommentVNode("", true)
                                        ])
                                    ], 10, _hoisted_14);
                                }), 128))
                            ])
                        ], 512), [
                            [vShow, unref(sorted).length > 0]
                        ]),
                        !unref(sorted) || unref(sorted).length === 0 ? (openBlock(), createElementBlock("div", _hoisted_28, [
                            createVNode(_component_Empty, {icon: "i-carbon:border-none op60 text-1.4rem"}, {
                                default: withCtx(() => _cache[17] || (_cache[17] = [
                                    createBaseVNode("span", {class: "op60"}, "暂无任务", -1)
                                ])),
                                _: 1
                            })
                        ])) : createCommentVNode("", true)
                    ], 64))
                ]),
                createVNode(_sfc_main$2, {
                    modelValue: unref(showDetailDrawer),
                    "onUpdate:modelValue": _cache[4] || (_cache[4] = ($event) => isRef(showDetailDrawer) ? showDetailDrawer.value = $event : null),
                    job: unref(selectedJob)
                }, null, 8, ["modelValue", "job"])
            ]);
        };
    }
});
const JobList = /* @__PURE__ */ _export_sfc(_sfc_main$1, [["__scopeId", "data-v-e5b446fd"]]);
const _sfc_main = /* @__PURE__ */ defineComponent({
    __name: "index",
    setup(__props) {
        const route = useRoute();
        const isRoot = computed(() => route.path === "/job/list");
        return (_ctx, _cache) => {
            const _component_RouterView = resolveComponent("RouterView");
            return openBlock(), createElementBlock(Fragment, null, [
                createVNode(JobList),
                createBaseVNode("div", {
                    class: normalizeClass(["fixed bottom-0 left-0 right-0 top-0 z-10 flex overflow-hidden bg-black:60 transition-all", unref(isRoot) ? "pointer-events-none opacity-0" : "opacity-100"])
                }, [
                    createBaseVNode("div", {
                        class: "h-full min-w-100px flex-auto",
                        onClick: _cache[0] || (_cache[0] = ($event) => _ctx.$router.go(-1))
                    }),
                    createBaseVNode("div", {
                        class: normalizeClass(["h-full min-w-800px w-[calc(100%-200px)] transform overflow-hidden border-l border-base bg-base shadow-lg transition-transform duration-200", unref(isRoot) ? "translate-x-1/2" : "translate-x-0"])
                    }, [
                        (openBlock(), createBlock(Suspense, null, {
                            fallback: withCtx(() => _cache[1] || (_cache[1] = [
                                createTextVNode(" 加载中 ")
                            ])),
                            default: withCtx(() => [
                                createVNode(_component_RouterView, null, {
                                    default: withCtx(({Component}) => [
                                        (openBlock(), createBlock(resolveDynamicComponent(Component)))
                                    ]),
                                    _: 1
                                })
                            ]),
                            _: 1
                        }))
                    ], 2)
                ], 2)
            ], 64);
        };
    }
});
export {
    _sfc_main as default
};
